﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Console_AI_HanNuo
{
    class Program
    {
        static Stack<int> leftColumn = new Stack<int>();
        static Stack<int> centerColumn = new Stack<int>();
        static Stack<int> rightColumn = new Stack<int>();

        static int level = 0;
        static void Main(string[] args)
        {
            InitializeCollection();

            PrintHanNuo();

            AIHanNuo(level, "1", "2", "3"); 

            Console.ReadLine();
        }

        static void InitializeCollection()
        {
            Regex colRegex = new Regex(@"^[1-9]\d*$");

            bool isInputPass = false;
            do
            {
                Console.WriteLine("Please input the level:");
                var inputString = Console.ReadLine();

                if (colRegex.IsMatch(inputString))
                {
                    isInputPass = true;

                    for (int i = 0; i < (level = int.Parse(inputString)); i++)
                    {
                        leftColumn.Push(int.Parse(inputString) - i);
                    }
                }
                else
                {
                    Console.WriteLine("Error: Please Input the integer (bigger than 1) number.");
                    isInputPass = false;
                }

            } while (!isInputPass);
        }

        static void PrintHanNuo()
        {
            int[] counts = new int[] { leftColumn.Count, centerColumn.Count, rightColumn.Count };
            var useLeftColumn = leftColumn.ToList();
            var useCenterColumn = centerColumn.ToList();
            var useRightColumn = rightColumn.ToList();

            int maxValue = 0;
            for (int i = 0; i < (maxValue = counts.Max()); i++)
            {
                var v1 = i >= maxValue - useLeftColumn.Count ? useLeftColumn[i - maxValue + useLeftColumn.Count].ToString() : "|";
                var v2 = i >= maxValue - useCenterColumn.Count ? useCenterColumn[i - maxValue + useCenterColumn.Count].ToString() : "|";
                var v3 = i >= maxValue - useRightColumn.Count ? useRightColumn[i - maxValue + useRightColumn.Count].ToString() : "|";

                Console.WriteLine(string.Format("{0}\t{1}\t{2}", v1, v2, v3));
            }

            Console.ReadLine();
        }

        static void Move(string sourceCol, string targetCol)
        {
            int? sourceValue = null;
            switch (sourceCol)
            {
                case "1":
                    sourceValue = leftColumn.Pop();
                    break;
                case "2":
                    sourceValue = centerColumn.Pop();
                    break;
                case "3":
                    sourceValue = rightColumn.Pop();
                    break;
                default:
                    break;
            }

            switch (targetCol)
            {
                case "1":
                    leftColumn.Push(sourceValue.Value);
                    break;
                case "2":
                    centerColumn.Push(sourceValue.Value);
                    break;
                case "3":
                    rightColumn.Push(sourceValue.Value);
                    break;
                default:
                    break;
            }
        }

        static Stack<int> GetColByID(string id)
        {
            switch (id)
            {
                case "1":
                    return leftColumn;

                case "2":
                    return centerColumn;

                case "3":
                    return rightColumn;

                default:
                    return new Stack<int>();
            }
        }

        static void AIHanNuo(int n, string sourceCol, string helpCol, string endCol)
        {
            if (n == 1)
            {
                Move(sourceCol, endCol);
                PrintHanNuo();

                if (leftColumn.Count == 0 && centerColumn.Count == 0)
                {
                    Console.WriteLine("Winner");
                    return;
                }
            }
            else
            {
                AIHanNuo(n - 1, sourceCol, endCol, helpCol);
                Move(sourceCol, endCol);
                PrintHanNuo();
                AIHanNuo(n - 1, helpCol, sourceCol, endCol);
            }
        }
    }
}
